package com.mMap.controller;

import com.mMap.annotation.GlobalInterceptor;
import com.mMap.annotation.VerifyParam;
import com.mMap.entity.constants.Constants;
import com.mMap.entity.dto.SessionWebUserDto;
import com.mMap.entity.enums.UserStatusEnum;
import com.mMap.entity.enums.VerifyRegexEnum;
import com.mMap.entity.po.OrderInfo;
import com.mMap.entity.po.UserInfo;
import com.mMap.entity.query.OrderInfoQuery;
import com.mMap.entity.query.UserInfoQuery;
import com.mMap.entity.vo.ResponseVO;
import com.mMap.exception.BusinessException;
import com.mMap.service.OrderInfoService;
import com.mMap.service.UserInfoService;
import com.mMap.service.impl.UserInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


@RestController
public class OrderController extends ABaseController{
    @Resource
    private OrderInfoService orderInfoService;
    @Autowired
    private UserInfoServiceImpl userInfoService;

    @RequestMapping("/topUpMoneyById")
    @GlobalInterceptor(checkLogin=true)
    public ResponseVO topUpMoneyById(HttpSession session,@VerifyParam(required = true) String id,@VerifyParam(required = true) String price) {
        try {
            System.out.println("id:"+id);
            System.out.println("price:"+price);
            SessionWebUserDto sessionWebUserDto = getUserInfoFromSession(session);
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setPrice(price);
            orderInfo.setCreator(sessionWebUserDto.getNickName());
            orderInfo.setCreatorId(sessionWebUserDto.getUserId());
            orderInfo.setCreateTime(new Date());
            orderInfo.setStatus(UserStatusEnum.ENABLE.getStatus());

            System.out.println(orderInfo);
            System.out.println("userId:"+id);
            UserInfo user = userInfoService.getUserInfoByUserId(id);
            System.out.println("user:"+user);
            System.out.println(user.getUserId()+":"+id);
            System.out.println(user.getUserId().equals(id));
            if (user.getUserId().equals(id)) {
                orderInfoService.add(orderInfo);
                System.out.println(user.getBalance());
                UserInfo userInfo = new UserInfo();
                if (user.getRole()==0) {
                    userInfo.setRole(2);
                    sessionWebUserDto.setRole(2);
                }
                userInfo.setBalance(Integer.parseInt(price)+user.getBalance());
                System.out.println("userInfo.setBalance:"+userInfo.getBalance());
                    sessionWebUserDto.setBalance(userInfo.getBalance());
                userInfoService.updateUserInfoByUserId(userInfo, sessionWebUserDto.getUserId());

                session.setAttribute(Constants.SESSION_KEY, sessionWebUserDto);
                return getSuccessResponseVO(null);
            } else return getErrorResponseVO(null,"没找到用户");

        } finally {
            session.removeAttribute(Constants.CHECK_CODE_KEY);
        }
    }
}

